<%@Language=VBScript %>
<% Option Explicit %>

<%

dim strPageHeading

strPageHeading = "User Registration"

%>

<!--#include file=../includes/pageaccess.asp-->

<%

%>

<!-- #INCLUDE FILE="../includes/ADOVBS.INC" -->



<HTML>
<!-- #INCLUDE FILE="../eventhandling/eventhandler.asp" -->
<HEAD>

<%
Const csFormName = "REGISTER"


Dim numComId
Dim sUserName
Dim sPassword
Dim sFullName
Dim sAddr1
Dim sAddr2
Dim sAddr3
Dim sAddr4
Dim sPostCode
Dim sDialingCode
Dim sTelephoneNo
Dim sEmail
Dim sBusUnitValues
Dim sBusUnitCD
Dim sPasswordType
Dim sOverrideUserName
Dim sForcePasswordChangeYN
Dim sUserTypeCD
Dim sCreatedBy
Dim sDefaultForm
Dim nDefaultFund
Dim sErrorMessage
Dim sSuccessMessage
Dim sCheckAllow

sErrorMessage = ""
sSuccessMessage = ""

' Initialization
Call Main()

'*---------------------------------------------------------------------
'* Method:			Main
'* Description: Page Entry Point
'* Parameters:	None
'* Returns:			Nothing
'*---------------------------------------------------------------------
Private Sub Main()
	' Who posted data to us - this form or another?
	' Use this logic because this form gets data from another (REGISTER_METHOD)
	' as well as itself.
	If Request.Form("txtFormName") = csFormName Then 'ie: register.asp - this form'
		' Get data from this form
		Call GetThisFormFields()
		' Build the busniness units combo box
		sBusUnitValues = GetBusinessUnit(numComId, sBusUnitCD ,sCheckAllow)

		' Has all the data been captured?
		sErrorMessage = ValidateFormFields()
		
		' If there were no errors then add the data to the database
		If sErrorMessage = "" Then
		  sErrorMessage = RegisterUser()
		  
		  If sErrorMessage = "" Then
				sSuccessMessage = "User successfully added."
		  End If
		End If ' if strMessage = ""
		
	Else
		if Request.Form("txtFormName") = "REGISTER_METHOD"  then 'We got here from REGISTER_METHOD.ASP
			' Get data from calling form
			Call GetCallingFormFields()

			'Only list the busniess units that allow external registration
			sCheckAllow = "Y"
		
			' Build the busniness units combo box
			sBusUnitValues = GetBusinessUnit(numComId, "" ,sCheckAllow)
		
		else 'We got here form user.asp, the user is logged on and adding another user.
			
			'Get the current users company id 
			Call GetThisUsersCompany(numComId)

			'When a user is adding a user, we will list all business units
			sCheckAllow = "N"
			
			' Build the busniness units combo box
			sBusUnitValues = GetBusinessUnit(numComId, "" ,sCheckAllow)
		
		end if

	End If ' 	If Request.Form("txtFormName") = csFormName Then
End Sub ' Main

'*---------------------------------------------------------------------
'* Method:			GetThisFormFields
'* Description:     Retrieve data from this page
'* Parameters:	    None
'* Returns:			Nothing
'*---------------------------------------------------------------------
Private Sub GetThisFormFields
	numComId = Request.Form("numComId")
	sBusUnitCD = Request.Form("lstBusUnit")
	sUsername = Request.Form("strUserName")
	sPassword = Request.Form("strPassword")
	sFullName	= Request.Form("strFullName")
	sAddr1 = Request.Form("strAddr1")
	sAddr2 = Request.Form("strAddr2")
	sAddr3 = Request.Form("strAddr3")
	sAddr4 = Request.Form("strAddr4")
	sPostCode = Request.Form("strPostCode")
	sDialingCode = Request.Form("numDialingCode")
	sTelephoneNo = Request.Form("numTelNo")
	sEmail = Request.Form("strEmail")
	sCheckAllow = Request.Form("sCheckAllow")
	
End Sub ' GetFormFields

'*---------------------------------------------------------------------
'* Method:			GetCallingFormFields
'* Description:     Retrieve data from calling form
'* Parameters:	    None
'* Returns:			Nothing
'*---------------------------------------------------------------------
Private Sub GetCallingFormFields()
	numComId = Request.Form("lstCompany") 
End Sub ' GetCallingFormFields()

'*---------------------------------------------------------------------
'* Method:			GetThisUsersCompany
'* Description:     Retrieve comid for user
'* Parameters:	    None
'* Returns:			Nothing
'*---------------------------------------------------------------------
Private Sub GetThisUsersCompany(numComId)
	
	Dim dllComID
	
	Set dllComId = Server.CreateObject("keytax.clsUsers")
	dllComID.GetUserComID session("KeyTaxUser"),numComId

End Sub ' GetThisUsersCompany()

'*---------------------------------------------------------------------
'* Method:			ValidateFormFields
'* Description:     Check if all mandatory fields have been catpured
'* Parameters:	    None
'* Returns:			String - An error message describing which fields
'*                       have not been captured
'*---------------------------------------------------------------------
Private Function ValidateFormFields
	Dim sValidateMessage
	dim sValidEmail
	
	' Intialize
	sValidateMessage = ""
	
	if sBusUnitCD = "" then
		sValidateMessage = "Please select a business unit for this user. <br>"
	end if
	
	' Check User Name
	If sUserName = "" Then
		sValidateMessage = sValidateMessage & "Username is missing" & "<BR>"
	Else
	  
	  'check that the user name is longer than 4 characters
	  If Len(sUsername) < 4 Then
	    sValidateMessage = sValidateMessage & "Username must be at least 4 characters long" & "<BR>"
	  End If
	  
	  'check that the username does not contain any spaces
	  if inSTR(sUsername," ") then
		sValidateMessage = "Username can not contain spaces" & "<br>"
	  end if
	End If
	
	' Check the Full Name
	If sFullName = "" Then
		sValidateMessage = sValidateMessage & "Full name is missing" & "<BR>"
	End If

	' Check address lines
	If sAddr1 = "" Then
	  sValidateMessage = sValidateMessage & "Location or Address is missing" & "<BR>"
	End If

	' Check dialing code
	If sDialingCode = "" Then
	  sValidateMessage = sValidateMessage & "Dialing Code is missing" & "<BR>"
	End If

	' Check telephone number
	If sTelephoneNo = "" Then
	  sValidateMessage = sValidateMessage & "Telephone Number is missing" & "<BR>"
	End If

	' Check for email address
	If sEmail = "" Then
		sValidateMessage = sValidateMessage & "Email address is missing" & "<BR>"
	else
		dim sEmailAtPos
		dim sEmailDotPos
		
		sEmailAtPos = instr(sEmail,"@")
		sEmailDotPos = instr(sEmail,".")
		
		if sEmailAtPos = 0 or sEmailAtPos = 1 then
			sValidEmail = "No"
		end if
		
		if sEmailDotPos = 0 or sEmailDotPos = 1 then
			sValidEmail = "No"
		end if
		
		if sValidEmail = "No" then
			sValidateMessage = sValidateMessage & "Email address is not valid" & "<br>"
		end if
		
	End If
	
	' Return the error message if any
	ValidateFormFields = sValidateMessage
	
End Function '  ValidateFormFields

'*---------------------------------------------------------------------
'* Method:			GetBusinessUnit
'* Description:     For a given company ID, get the business units that
'*                  belong to it
'* Parameters:	    iCompanyID - Company ID value
'*                  sSelectedBusUnit -
'* Returns:			String - An HTML OPTION list of business units
'*---------------------------------------------------------------------
Private Function GetBusinessUnit(ByVal numComId, _
                                 ByVal sSelectedBusUnit, _ 
                                 ByVal sCheckAllow)

	On Error Resume Next
                                 
	Dim dllBusUnit
	' Return the OPTION list
	Set dllBusUnit = Server.CreateObject("keytax.clsUsers")
	GetBusinessUnit = dllBusUnit.GetBusinessUnit(numComId,CStr(sSelectedBusUnit),CStr(sCheckAllow))
End Function ' GetBusinessUnit

'*---------------------------------------------------------------------
'* Method:			RegisterUser
'* Description:     Add the captured details to the database
'* Parameters:	    None
'* Returns:			String - A message to indicate success/failure
'*---------------------------------------------------------------------
Private Function RegisterUser()
	Dim sRegMsg
	Dim sAuthorisedYN
	Dim iCSLevID
	
	' Reset the message
	sRegMsg  = ""
	
	' Get the defaults for the business unit that was selected
	sRegMsg = sRegMsg & GetBusinessUnitDefaults(numComID, _
	                                            sBusUnitCD, _
	                                            sAuthorisedYN, _
	                                            iCSLevID)
	                              
	sRegMsg = sRegMsg & InsertUser(sAuthorisedYN, iCSLevID)	                             
	                              
	' Return the message to be displayed to the user showing the outcome
	RegisterUser = sRegMsg
End Function ' RegisterUser

'*---------------------------------------------------------------------
'* Method:			GetBusinessUnitDefaults
'* Description: 
'* Parameters:	   numComId - Company ID
'*                 sAuthorisedYN - Returned value
'*                 iCSLevID - ReturnedValue
'* Returns:		   String
'*---------------------------------------------------------------------
Private Function GetBusinessUnitDefaults(ByVal numComId, _
                                         ByVal sBusUnitCD, _
                                         ByRef sAuthorisedYN, _
                                         ByRef iCSLevID)
	Dim dllGetBusUnitDef
on error resume next
	Set dllGetBusUnitDef = Server.CreateObject("keytax.clsUsers")
	GetBusinessUnitDefaults = dllGetBusUnitDef.GetBusinessUnitDefaults(CStr(numComId), _
																	   CStr(sBusUnitCD), _
																	   sOverrideUserName, _
																	   iCSLevID, _
																	   sAuthorisedYN, _
																	   sPasswordType, _
																	   sForcePasswordChangeYN, _
																	   sUserTypeCD, _
																	   sCreatedBy, _
																	   sDefaultForm, _
																	   nDefaultFund)
                      
End Function ' GetBusinessUnitDefaults

'*---------------------------------------------------------------------
'* Method:			InsertUser
'* Description: 
'* Parameters:	    sAuthorisedYN
'*                  iCSLevID
'* Returns:			Nothing
'*---------------------------------------------------------------------
Private Function InsertUser(ByVal sAuthorisedYN, _
                            ByVal iCSLevID)
	
	                       
	Dim dllInsertUser
	'Response.Write "<br>" & numComId & " , " & sUserName& " , " & sPasswordType& " , " & sOverrideUserName& " , " & iCSLevID& " , " & sForcePasswordChangeYN& " , " & sFullName& " , " & sDialingCode& " , " & sTelephoneNo& " , " & sEmail& " , " & sAddr1& " , " & sAddr2& " , " & sAddr3& " , " & sAddr4& " , " & sPostCode& " , " & sCreatedBy& " , " & sUserTypeCD& " , " & sBusUnitCD& " , " & sDefaultForm& " , " & nDefaultFund& " , " & sAuthorisedYN
	Set dllInsertUser = Server.CreateObject("keytax.clsUsers")
	InsertUser = dllInsertUser.InsertUser(CStr(numComId), _
										  CStr(sUserName), _
										  CStr(sPasswordType), _
                                          CStr(sOverrideUserName), _
                                          CStr(iCSLevID), _
                                          CStr(sForcePasswordChangeYN), _
                                          CStr(sFullName), _
                                          CStr(sDialingCode), _
                                          CStr(sTelephoneNo), _
                                          CStr(sEmail), _
                                          CStr(sAddr1), _
                                          CStr(sAddr2), _
                                          CStr(sAddr3), _
                                          CStr(sAddr4), _
                                          CStr(sPostCode), _
                                          CStr(sCreatedBy), _
                                          CStr(sUserTypeCD), _
                                          CStr(sBusUnitCD), _
                                          CStr(sDefaultForm), _
                                          nDefaultFund, _
                                          CStr(sAuthorisedYN))

End Function ' InsertUser                                    
%>

<% 
	if sCheckAllow = "Y" then

	strPageHeading = "Online Registration"

	end if 
%>

<title>Keytax - <%=strPageHeading%></title>

<!--#include file=../eventhandling/eventhandler.asp-->

</HEAD>
<link rel="stylesheet" type="text/css" href="../../stylesheets/keytaxv2.css">

<body language="vbscript" topmargin=10 >

<table width="100%" cellpadding=0 cellspacing=0>
	<tr>
		<td><!--#include file=../menu/menu.asp--></td>
	</tr>
	<tr>
		<td class=PageHeadingCell><%=strPageHeading%> &nbsp&nbsp</td>
	</tr>
	<tr>
		<td></td>
	</tr>
</table>


<form name="frmRegister" method=post action="register.asp">

<br>

<% 
		'write the error message, if any.
		
		If sErrorMessage <> "" Then
		
		Response.Write	"<table align=center class=FormTable cellspacing=1 cellpadding=2 width=400>" & _
							"<tr>" &_
								"<td class=FormHeadingCell>Messages:" &_
							"</tr>" &_
							"<tr>" &_
								"<td class=FormLeftCell align=center><br><center><b><font color=red>" & sErrorMessage & "</font></b></center><br></td>" &_
							"</tr>" &_
						"</table>"
		End If
	%>

	<%
		'write the success message, if any.
		
		If sSuccessMessage <> "" Then
		
		Response.Write	"<table align=center class=FormTable cellspacing=1 cellpadding=2 width=400>" & _
							"<tr>" &_
								"<td class=FormHeadingCell>Messages:" &_
							"</tr>" &_
							"<tr>" &_
								"<td class=FormLeftCell align=center><br><center><b><font color=green>" & sSuccessMessage & "</font></b></center><br></td>" &_
							"</tr>" &_
						"</table>"
		
		End If
	%>
	
<br>

<table class="FormTable" cellpadding=2 cellspacing=1 align=center border=0>
	<tr>
		<td class="FormHeadingCell" colspan=3>Please fill in the following:</td>
	</tr>
	<tr>
		<td class="FormSubHeadingCell" valign=top colspan=3><font color=red>*</font>denotes mandatory fields</td>
	</tr>
	<tr>
		<td class="FormLeftCell">Business area:</td>
		<td class="FormRightCell">
			&nbsp&nbsp<select name=lstBusUnit style="width:350px" >
				<option value=""></option>
				<%=sBusUnitValues %>
			</select>
		</td>
	</tr>
	<tr>
		<td class="FormLeftCell">Username:  <font size=1>(To log onto the system with - please do not use spaces.)</font></td>
		<td class="FormRightCell"><font color=red>*</font><input onkeypress="nospaces()" name="strUsername" style="width:350px" maxlength=10 value="<%= Request.Form("strUserName") %>" ></td>
	</tr>
	<tr>
		<td class="FormLeftCell">Full name:  <font size=1>(First name and surname.)</font></td>
		<td class="FormRightCell"><font color=red>*</font><input name="strFullName" style="width:350px" maxlength=120  value="<%= Request.Form("strFullName") %>" ></td>
	</tr>
	<tr>
		<td class="FormLeftCell" rowspan=4 valign=top>
			Location / Address:
			
			<br><br>
			
			<font size=1>(Where the KeyTax system will be used from.)</font>
			
		</td>
		<td class="FormRightCell"><font color=red>*</font><input name="strAddr1" style="width:350px" maxlength=35 value="<%= Request.Form("strAddr1") %>" ></td>
	</tr>
	<tr>
		<td class="FormRightCell">&nbsp&nbsp<input name="strAddr2" style="width:350px" maxlength=35  value="<%= Request.Form("strAddr2") %>" ></td>
	</tr>
	<tr>
		<td class="FormRightCell">&nbsp&nbsp<input name="strAddr3" style="width:350px" maxlength=35 value="<%= Request.Form("strAddr3") %>" ></td>
	</tr>
	<tr>
		<td class="FormRightCell">&nbsp&nbsp<input name="strAddr4" style="width:350px" maxlength=35 value="<%= Request.Form("strAddr4") %>" ></td>
	</tr>
	<tr>
		<td class="FormLeftCell">Postal Code:</td>
		<td class="FormRightCell">&nbsp&nbsp<input name="strPostCode" style="width:350px" maxlength=10 value="<%= Request.Form("strPostCode") %>" ></td>
	</tr>
	<tr>
		<td class="FormLeftCell">Day-time telephone number: <font size=1>(code - number)</font>:</td>
		<td class="FormRightCell"><font color=red>*</font><input name="numDialingCode" style="width:30%" maxlength=10 value="<%= Request.Form("numDialingCode") %>" onkeypress="numbersonly()"> - <input name="numTelNo" style="width:62%" maxlength=10 value="<%= Request.Form("numTelNo") %>" onkeypress="numbersonly()"></td>
	</tr>
	<tr>
		<td class="FormLeftCell">E-mail address: <font size=1>(This is where the Directive will be e-mailed to)</font></td>
		<td class="FormRightCell"><font color=red>*</font><input name="strEmail" style="width:350px" maxlength=80 value="<%= Request.Form("strEmail") %>"></td>
	</tr>
    <tr>
		<td class="FormBottomBar" colspan=3><input class="FormBottomButton" name="btnSubmit" type=submit value="Submit >>"></td>
	</tr>
</table>

<tr><td><input type="hidden" name=numComId value="<%= numComId%>" ></td></tr>
<tr><td><input type="hidden" name=strBusUnitValues value="<%= sBusUnitValues%>" ></td></tr>
<tr><td><input type="hidden" name=txtFormName value="<%= csFormName %>" ></td></tr>
<tr><td><input type="hidden" name=sCheckAllow value="<%= sCheckAllow %>" ></td></tr>

</form>
</BODY>
</HTML>
